var express = require('express');
var router = express.Router();
var md5 = require("md5");
var ccap = require("ccap");

var getDate = require("../bin/tools/time.js");
var userDAO = require("../bin/userDAO.js");

var captcha = ccap({
    width: 130,
    height: 40,
    offset: 33,
    quality: 100,
    fontsize: 40,
    generate: function() {
        var sourceStr = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        var text = "";
        for (var i = 0; i < 4; i++) {
            var index = Math.floor(Math.random() * sourceStr.length);
            var ch = sourceStr[index];
            text += ch;
        }
        return text;
    }
});

router.get("/captcha", (req, res) => {
    var arr = captcha.get();
    console.log(arr[0]);
    req.session.captcha = arr[0];
    res.set("Content-Type", "image/jpeg");
    res.send(arr[1]);
});


router.post('/regist', function(req, res) {
    var reg = /^1[3|5|7|8]\d{9}$/;
    if (req.body.captcha.toUpperCase() != req.session.captcha) {
        res.json({ err: 1, msg: '验证码错误' });
        return;
    }
    userDAO.findOne({ tel: req.body.tel })
        .then(data => {
            if (data) {
                res.json({ err: 2, msg: '该手机号已被注册' });
            } else {
                if (!reg.test(req.body.tel)) {
                    res.json({ err: 3, msg: '手机号码格式不正确' });
                } else {
                    req.body.psw = md5(req.body.psw);
                    var u = new userDAO({
                        tel: req.body.tel,
                        psw: req.body.psw
                    });
                    u.save()
                    .then(data => {
                        res.json({ err: 0, msg: '注册成功'});
                    })
                }
            }
        });

});

router.post("/login", (req, res) => {
    req.body.psw = md5(req.body.psw);
    userDAO.findOne(req.body)
    .then(data=>{
      if(!data){
        res.json({err:1,msg:'账号或密码错误'});
      }else{
        req.session.user = data;
        delete req.session.user.psw;
        res.json({err:0,msg:'登录成功', userInfo: req.session.user });
      }
    });
    
});

// 判断是否登录
router.get("/islogin", (req, res) => {
    if (req.session.user) {
        res.json({ err: 0, userInfo: req.session.user });
    }
});
router.post("/order",(req,res)=>{
    userDAO.findOne({tel:req.session.user.tel})
    .then(data=>{
        req.body.date = getDate(req.body.nowTime);
        data.order.push(req.body);
        data.save()
        .then(data2=>{
            res.json({err:0});
        });
    });
});
router.post("/oneorder",(req,res)=>{
    userDAO.findOne({tel:req.session.user.tel})
    .then(data=>{
        var dingdan
        data.order.forEach(function(e) {
            if(e.nowTime==req.body.time){
                dingdan = e;
                return;
            }
        }, this);
        res.json({err:0,data:dingdan});
    });
});

router.get("/allOrder",(req,res)=>{
    userDAO.findOne({tel:req.session.user.tel})
    .then(data=>{
        res.json({err:0,data:data.order});
    });
});


// 退出登录
router.get("/exit", (req, res) => {
    req.session.user = null;
    res.json({ err: 0, msg: "退出成功" });
});

module.exports = router;